package com.lt.app;

import android.app.Activity;
import android.content.ComponentName;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.text.TextUtils;

import com.lt.base.BaseApplication;
import com.lt.LibConstant;

public class AppHelper {
    private static AppHelper instance;

    private AppHelper() {
    }

    public static AppHelper getInstance() {
        if (instance == null) {
            synchronized (AppHelper.class) {
                instance = new AppHelper();
            }
        }
        return instance;
    }

    /**
     * @return 获取app版本号
     */
    public String getAppVersionName() {
        String versionName = null;
        try {
            PackageManager pm = BaseApplication.getContext().getPackageManager();
            PackageInfo pi = pm.getPackageInfo(BaseApplication.getContext().getPackageName(), 0);
            versionName = pi.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return TextUtils.isEmpty(versionName) ? LibConstant.unknown : versionName;
    }

    /**
     * @return 获取app版本号
     */
    public int getAppVersionCode() {
        try {
            PackageManager pm = BaseApplication.getContext().getPackageManager();
            PackageInfo pi = pm.getPackageInfo(BaseApplication.getContext().getPackageName(), 0);
            return pi.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取AndroidMainfest.xml里meta-data的value
     */
    public Object getApplicationMetaDataValue(String metaKey) {
        Object value = null;
        PackageManager packageManager = BaseApplication.getContext().getPackageManager();
        ApplicationInfo info;
        try {
            info = packageManager.getApplicationInfo(BaseApplication.getContext().getPackageName(), PackageManager.GET_META_DATA);
            if (info != null && info.metaData != null) {
                value = info.metaData.get(metaKey);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return value;
    }

    public Object getActivityMetaDataValue(Activity act, String metaKey) {
        Object value = null;
        PackageManager packageManager = BaseApplication.getContext().getPackageManager();
        if (act != null) {
            ActivityInfo info;
            try {
                info = packageManager.getActivityInfo(act.getComponentName(), PackageManager.GET_META_DATA);
                if (info != null && info.metaData != null) {
                    value = info.metaData.get(metaKey);
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return value;
    }

    public Object getReceiverMetaDataValue(Class receiverCls, String metaKey) {
        Object value = null;
        PackageManager packageManager = BaseApplication.getContext().getPackageManager();
        ActivityInfo info;
        try {
            info = packageManager.getReceiverInfo(new ComponentName(BaseApplication.getContext(), receiverCls), PackageManager.GET_META_DATA);
            if (info != null && info.metaData != null) {
                value = info.metaData.get(metaKey);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return value;
    }

    public Object getServiceMetaDataValue(Class serviceCls, String metaKey) {
        Object value = null;
        PackageManager packageManager = BaseApplication.getContext().getPackageManager();
        ServiceInfo info;
        try {
            info = packageManager.getServiceInfo(new ComponentName(BaseApplication.getContext(), serviceCls), PackageManager.GET_META_DATA);
            if (info != null && info.metaData != null) {
                value = info.metaData.get(metaKey);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return value;
    }

}